package com.baihan.angus.app.game.busshow.ui.main;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.baihan.angus.app.game.basic.base.BaseActivity;
import com.baihan.angus.app.game.basic.configs.BasicConfig;
import com.baihan.angus.app.game.basic.event_bus.BusEventCode;
import com.baihan.angus.app.game.basic.event_bus.LiveDataEvent;
import com.baihan.angus.app.game.basic.utils.GenerateTestUserSig;
import com.baihan.angus.app.game.basic.utils.Loog;
import com.baihan.angus.app.game.busshow.BR;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.busshow.databinding.ActivityMainBinding;
import com.baihan.angus.app.game.busshow.ui.home.HomeFragment;
import com.baihan.angus.app.game.busshow.ui.msg.MsgFragment;
import com.baihan.angus.app.game.busshow.ui.script_library.ScriptDepotFragment;
import com.baihan.angus.app.game.busshow.ui.user.UserFragment;
import com.baihan.angus.app.game.common.arouter_path.ArouterAppPath;
import com.baihan.angus.app.game.common.bean.response.Userinfo;
import com.baihan.angus.app.game.common.configs.UserConfig;
import com.baihan.angus.app.game.common.model.TRTCVoiceRoom;
import com.baihan.angus.app.game.common.model.TRTCVoiceRoomCallback;
import com.google.gson.Gson;
import com.gyf.immersionbar.ImmersionBar;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

@Route(path = ArouterAppPath.MAIN_ACTIVITY)
public class MainActivity extends BaseActivity<ActivityMainBinding, MainVM> {

    private final String TAG_0 = "0";
    private final String TAG_1 = "1";
    private final String TAG_2 = "2";
    private final String TAG_3 = "3";

    private Fragment mCurrentFragment;
    private HomeFragment mHomeFragment;
    private ScriptDepotFragment mScriptDepotFragment;
    private MsgFragment mMsgFragment;
    private UserFragment mUserFragment;

    private TRTCVoiceRoom mTRTCVoiceRoom;
    private Userinfo userInfo;
    private String userSig;

    @Override
    protected int getContentLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initStatusBar() {
        ImmersionBar.with(this).fitsSystemWindows(false).transparentStatusBar().statusBarDarkFont(BasicConfig.getInstance().isDayMode()).init();
    }

    @Override
    protected int getViewModelId() {
        return BR.mainVM;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        EventBus.getDefault().register(this);

        mVDBinding.btnHome.setOnClickListener(mOnMenuClickListener);
        mVDBinding.btnScriptLibrary.setOnClickListener(mOnMenuClickListener);
        mVDBinding.btnMsg.setOnClickListener(mOnMenuClickListener);
        mVDBinding.btnUser.setOnClickListener(mOnMenuClickListener);

        mHomeFragment = HomeFragment.newInstance();
        mScriptDepotFragment = ScriptDepotFragment.newInstance();
        mMsgFragment = MsgFragment.newInstance();
        mUserFragment = UserFragment.newInstance();

//        mVDBinding.rgMain.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(RadioGroup group, int checkedId) {
//                tabSelect(checkedId);
//            }
//        });
//        mVDBinding.rbExhibition.setChecked(true);
    }

    @Override
    protected void initData() {

        userInfo = UserConfig.getUserConfig().getUser();
        userSig = UserConfig.getUserConfig().getSavedUserSig();

        Loog.dTag("UserInfoMain", "userInfo:" + new Gson().toJson(userInfo));

        initUI();
    }

    private void initUI() {

        mVDBinding.btnHome.setEnabled(false);
        mVDBinding.btnScriptLibrary.setEnabled(true);
        mVDBinding.btnMsg.setEnabled(true);
        mVDBinding.btnUser.setEnabled(true);
        switchFragment(mHomeFragment, TAG_0);

        mTRTCVoiceRoom = TRTCVoiceRoom.sharedInstance(this);
        mTRTCVoiceRoom.login(GenerateTestUserSig.SDKAPPID, userInfo.getId(), userSig, new TRTCVoiceRoomCallback.ActionCallback() {
            @Override
            public void onCallback(int code, String msg) {
                if (code == 0) {
//                    mTRTCVoiceRoom.setSelfProfile(userInfo.getNickname(), userInfo.getAvatar(), new TRTCVoiceRoomCallback.ActionCallback() {
//                        @Override
//                        public void onCallback(int code, String msg) {
//                            if (code == 0) {
//                                Loog.dTag("UserLogin:", "setSelfProfile success");
//                            }
//                        }
//                    });
                }
            }
        });
    }

    private void switchFragment(final Fragment targetFragment, String tag) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (!targetFragment.isAdded()) {
            if (mCurrentFragment != null) {
                transaction.hide(mCurrentFragment);
            }
            transaction.add(R.id.flContent, targetFragment, tag);
        } else {
            if (targetFragment == mCurrentFragment) return;
            transaction.hide(mCurrentFragment);
            transaction.show(targetFragment);
        }
        mCurrentFragment = targetFragment;
        transaction.commit();
    }

    private View.OnClickListener mOnMenuClickListener = v -> {

        if (!TextUtils.isEmpty(mCurrentFragment.getTag())) {
            switch (mCurrentFragment.getTag()) {
                case TAG_0:
                    mVDBinding.btnHome.setEnabled(true);
                    break;
                case TAG_1:
                    mVDBinding.btnScriptLibrary.setEnabled(true);
                    break;
                case TAG_2:
                    mVDBinding.btnMsg.setEnabled(true);
                    break;
                case TAG_3:
                    mVDBinding.btnUser.setEnabled(true);
                    break;
            }
        }
        switch (v.getId()) {
            case R.id.btnHome:
                mVDBinding.btnHome.setEnabled(false);
                switchFragment(mHomeFragment, TAG_0);
                break;
            case R.id.btnScriptLibrary:
                mVDBinding.btnScriptLibrary.setEnabled(false);
                switchFragment(mScriptDepotFragment, TAG_1);
                break;
            case R.id.btnMsg:
                mVDBinding.btnMsg.setEnabled(false);
                switchFragment(mMsgFragment, TAG_2);
                break;
            case R.id.btnUser:
                mVDBinding.btnUser.setEnabled(false);
                switchFragment(mUserFragment, TAG_3);
                break;
        }
    };

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(LiveDataEvent liveDataEvent) {
        switch (liveDataEvent.what) {
            case BusEventCode.EVENT_AUTH_401:
                ARouter.getInstance().build(ArouterAppPath.LOGIN_ACTIVITY).navigation();
                break;
        }
    }

    @Override
    protected void initObservers() {
        super.initObservers();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}